/*
 * PureMVC - Copyright(c) 2006-08 Futurescale, Inc., Some rights reserved.
 * Your reuse is governed by Creative Commons Attribution 3.0 License
 */

package org.puremvc4gwt.client.patterns.observer;

import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

import org.puremvc4gwt.client.interfaces.INotification;
import org.puremvc4gwt.client.interfaces.IObserver;

/**
 * Helper class that contains all observers for a notification
 * 
 * @see org.puremvc4gwt.client.interfaces IObservers
 * @see org.puremvc4gwt.client.interfaces INotification
 */
public class Observers {

    private Vector<IObserver> observers; // see View.registerObservers, "in the
                                        // order in which they were registered"

    private String notificationName;

    /**
     * Constructor
     * 
     * @param note
     * @param observer
     */
    public Observers(String note, IObserver observer) {
        if (note != null && observer != null) {
            this.observers = new Vector<IObserver>();
            this.observers.add(observer);
            this.notificationName = note;
        } else {
            throw new NullPointerException();
        }
    }

    /**
     * Returns all observers
     * 
     * @return <code>List<IObserver></code>
     */
    public List<IObserver> getObservers() {
        return this.observers;
    }

    /**
     * Add new observer
     * 
     * @param observer
     */
    public void addObserver(IObserver observer) {
        if (observer != null) {
            this.observers.add(observer);
        } else {
            throw new NullPointerException();
        }
    }

    /**
     * Delete an observer
     * 
     * @param observer
     */
    public void deleteObserver(IObserver observer) {
        if (observer != null) {
            this.observers.remove(observer);
        } else {
            throw new NullPointerException();
        }
    }

    /**
     * Notify all observers of the notification
     * 
     * @param note
     */
    public void notifyObservers(INotification note) {
        if (note != null) {
            IObserver temp = null;
            for (Enumeration<IObserver> enu = this.observers.elements(); enu.hasMoreElements();) {
                temp =  enu.nextElement();
                temp.notifyObserver( note );
            }
        } else {
            throw new NullPointerException();
        }
    }

    /**
     * Return the notification
     * 
     * @return String
     */
    public String getNotification() {
        return this.notificationName;
    }
}
